from datetime import date, timedelta

from rest_framework import serializers
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.goods.models import GoodsCategory, GoodsVisitCount
from apps.meiduo_admin.serializers.goodserializer import GoodsSerializer
from apps.orders.models import OrderInfo
from apps.users.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

class UserDailyActiveCountView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    # permission_classes = [IsAdminUser]

    def list(self, request, *args, **kwargs):
        # 获取当前日期
        now_date = date.today()
        # 获取活跃用户总数
        count = User.objects.filter(last_login__gte=now_date).count()
        return Response({
            'count': count,
            'date': now_date
        })
class UserOrderCountView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        now_date = date.today()
        count = User.objects.filter(orderinfo__create_time__gte=now_date).distinct()
        # data = OrderInfo.objects.all().values_list('user_id').distinct()
        # return Response({'count':data.count(),'date':now_date})
        return Response ({
            'count':count.count(),
            'now_date':now_date
        })

class UUU(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        now_date = date.today()
        count = User.objects.filter(orderinfo__create_time__gte=now_date).distinct().count()
        data = OrderInfo.objects.all().values_list('user_id').distinct()
        # return Response({'count':data.count(),'date':now_date})
        return Response ({
            'count':count,
            'now_date':now_date
        })

#日增用户数量显示
class DailyAddView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        now_date = date.today()
        count = User.objects.filter(last_login__gt=now_date).count()
        return Response({
            'count':count,
            'now_date':now_date
        })
#用户总数显示
class TotalView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        count = User.objects.all().count()
        return Response({
            'count':count
        })



class EveryMonthUserView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        # 今天的日期
        now_date = date.today()
        # 一个月前的日期
        dateone = now_date - timedelta(days=30)
        print(dateone)

        data_list = []

        for i in range(30):
            #循环遍历当前的日期
            At_now = dateone + timedelta(days=i)
            # 指定下一天的日期
            last_day = dateone + timedelta(days= i+1)

            count = User.objects.filter(date_joined__gte=At_now,date_joined__lt=last_day).count()
            # count = User.objects.filter(date_joined__date=At_now).count()
            data_list.append({
                'count':count,
                'date':At_now
            })
        return Response(data_list)


class AA(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        now_date = date.today()
        # start_date = now_date - timedelta(days=30)
        list =[]
        for i in range(30):
            start = now_date - timedelta(days=i)
            count = User.objects.filter(date_joined__date=start).count()
            list.append({
                'count':count,
                'date':start
            })
            # list.insert(-1,{})
            list.reverse()
        return Response(list)





class GoodView(ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    def list(self, request, *args, **kwargs):
        # now_date = date.today()
        data = GoodsVisitCount.objects.all()
        list = []
        for i in data:
            list.append({'category':i.category.name,'count':i.count})
        # data = GoodsVisitCount.objects.filter(date=now_date)
        # ser=GoodsSerializer(data,many=True)
        # return Response(ser.data)
        return Response(list)


